<?php
/**
 * Created by PhpStorm.
 * User: HuangLuSen
 * Date: 2017/5/27
 * Time: 12:51
 */

namespace app\common\model;


use app\common\traits\WechatZjs;
use think\Model;

class SendHuaShua extends Model
{
    use WechatZjs;
    public function sendMsg($data) {
        if(!empty($data)){
            $orderdata = \think\Db::name('orders')->where(['id'=>$data['id']])->find();
            $user = \think\Db::name('customer')->where(['id'=>$orderdata['user_id']])->find();
            $info = \think\Db::name('huashu')->where( ['state_id'=>$orderdata['ywstate'],'status'=>1,'hslx'=>$data['hslx']] )->order('sort desc')->find();
            if(empty($info)){
                $info=[];
                $info['content']="尊敬的顾客,您推荐的客人 {$orderdata['name']} 的订单状态已发生变更!谢谢您对玛瑞莎的支持";
                $info['name']="状态变更";
            }else{
                $info['content'] = str_replace('{$user}',$orderdata['name'] ,$info['content']);
                if(isset($data['jujueliyou'])){
                    $info['content'] = str_replace('{$jujueliyou}','由于'.$data['jujueliyou'] ,$info['content']);
                }
            }
            $tempdata = [
                'touser' => "{$user['openid']}",
                'template_id' => config('admin.weixin.zjs_notice'),
                'url' => url('zjs/personal/recommend','',TRUE,TRUE),
                'data' => [
                    "first"    => array("{$info['content']}！", '#555555'),
                    "keyword1" => array("No.".$orderdata['id'], "#336699"),
                    "keyword2" => array("{$info['name']}", "#FF0000"),
                    "keyword3" => array(date("Y-m-d H:i", time()), "#888888"),
                    "remark"   => array("玛瑞莎感谢您的支持与关注！", "#5599FF"),
                ]
            ];
            $re = $this->_sendmsg($tempdata);
            if($re->errcode>0){
                addLog([
                    'order_id'=>$orderdata['id'],
                    'status'=>$info['name'],
                    'info'=>$info['content'],
                    'state'=>2,
                    'operation'=>6,
                ]);
//                return json(['status'=>FALSE,'info'=>'微信发送失败！:'.$re->errmsg]);
                return ['status'=>FALSE,'info'=>'微信发送失败！:'.$re->errmsg];
            }else{
                addLog([
                    'order_id'=>$orderdata['id'],
                    'status'=>$info['name'],
                    'info'=>$info['content'],
                    'operation'=>6,
                ]);
//                return json(['status'=>TRUE,'info'=>'微信发送成功！:'.$re->errmsg]);
                return ['status'=>TRUE,'info'=>'微信发送成功！:'.$re->errmsg];
            }
        }else{
//            return json(['status'=>false,'info'=>'提交的方式不对！']);
            return ['status'=>false,'info'=>'提交的方式不对！'];
        }
    }
    public function _sendmsg($data){
        $notice = self::getInstance()->notice;
        $id = $notice->send($data);
        return $id;
    }
}